package com.qbw.app.model;

import androidx.databinding.Bindable;
import android.text.TextUtils;

import com.google.gson.annotations.SerializedName;
import com.lib.util.ResUtil;
import com.lib.util.StringUtil;
import com.qbw.app.R;
import com.qbw.app.module.common.view.WaveProgressBar;

import java.io.Serializable;
import java.util.List;

/**
 * @author QBW
 * @date 2019/8/29
 */
public class Voltage extends BaseModel implements Serializable {

    public static final String NORMAL = "1";
    public static final String SIDE_LOW = "2";
    public static final String EXCEPTION = "-1";
    public static final String NO_DATA = "0";
    public static final String SIDE_HIGH = "3";
    public static final String LOW = "4";

    @Deprecated
    public enum Standard {
        /**
         * 低电
         */
        VOLTLOW,
        /**
         * 偏低
         */
        VOLTSIDELOW,
        /**
         * 正常
         */
        VOLTNORMAL,
        /**
         * 偏高
         */
        VOLTHIGH
    }

    private long lastUpdateTime;
    private StandardList standardList;
    /**
     * 电瓶电压状态，1：正常，0--（无数据） -1 异常 2 偏低 3 偏高 4 低电
     */
    private String state = NO_DATA;
    private double value;

    private Vehicle mVehicle;

    @Bindable
    public long getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void setLastUpdateTime(int lastUpdateTime) {
        this.lastUpdateTime = lastUpdateTime;
    }

    public StandardList getStandardList() {
        return standardList;
    }

    public void setStandardList(StandardList standardList) {
        this.standardList = standardList;
    }

    @Bindable
    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
        notifyPropertyChanged(com.qbw.app.BR.state);
    }

    @Bindable
    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public Vehicle getVehicle() {
        return mVehicle;
    }

    public void setVehicle(Vehicle vehicle) {
        mVehicle = vehicle;
    }

    public String getCurrentStandard() {
        if (standardList == null) {
            return "";
        }
        if (NORMAL.equals(state)) {
            return standardList.voltNormal;
        } else if (SIDE_LOW.equals(state)) {
            return standardList.voltSideLow;
        } else if (LOW.equals(state)) {
            return standardList.voltLow;
        } else if (SIDE_HIGH.equals(state)) {
            return standardList.voltHigh;
        }
        return "";
    }

    public static class StandardList extends BaseModel implements Serializable {
        /**
         * 低电
         */
        private String voltLow;
        /**
         * 偏低
         */
        @SerializedName("voltSideLoaw")
        private String voltSideLow;
        /**
         * 正常
         */
        private String voltNormal;
        /**
         * 偏高-范围值如：12.6#11.9，大数在前，小数在后，以#分开
         */
        private String voltHigh;

        @Bindable
        public String getVoltLow() {
            return voltLow;
        }

        public void setVoltLow(String voltLow) {
            this.voltLow = voltLow;
        }

        @Bindable
        public String getVoltSideLow() {
            return voltSideLow;
        }

        public void setVoltSideLow(String voltSideLow) {
            this.voltSideLow = voltSideLow;
        }

        @Bindable
        public String getVoltNormal() {
            return voltNormal;
        }

        public void setVoltNormal(String voltNormal) {
            this.voltNormal = voltNormal;
        }

        @Bindable
        public String getVoltHigh() {
            return voltHigh;
        }

        public void setVoltHigh(String voltHigh) {
            this.voltHigh = voltHigh;
        }

        @Deprecated
        public String getVoltageBelongTo(Standard standard) {
            switch (standard) {
                case VOLTLOW:
                    return voltLow;
                case VOLTSIDELOW:
                    return voltSideLow;
                case VOLTNORMAL:
                    return voltNormal;
                case VOLTHIGH:
                    return voltHigh;
                default:
                    return "";
            }
        }
    }

    @Deprecated
    public Standard getStandard() {
        Standard standard = Standard.VOLTNORMAL;
        if (standardList == null) {
            return standard;
        }
        List<Double> doubles = StringUtil.doubles(standardList.voltLow, "#");
        if (doubles == null || doubles.size() < 2) {
            return standard;
        }
        if (value >= doubles.get(0) && value <= doubles.get(1)) {
            standard = Standard.VOLTLOW;
            return standard;
        }
        doubles = StringUtil.doubles(standardList.voltSideLow, "#");
        if (doubles == null || doubles.size() < 2) {
            return standard;
        }
        if (value >= doubles.get(0) && value <= doubles.get(1)) {
            standard = Standard.VOLTSIDELOW;
            return standard;
        }
        doubles = StringUtil.doubles(standardList.voltNormal, "#");
        if (doubles == null || doubles.size() < 2) {
            return standard;
        }
        if (value >= doubles.get(0) && value <= doubles.get(1)) {
            standard = Standard.VOLTNORMAL;
            return standard;
        }
        doubles = StringUtil.doubles(standardList.voltHigh, "#");
        if (doubles == null || doubles.size() < 2) {
            return standard;
        }
        if (value >= doubles.get(0) && value <= doubles.get(1)) {
            standard = Standard.VOLTHIGH;
            return standard;
        }
        return standard;
    }

    public int getStatusDrawableByStandard(Standard standard) {
        int dr = R.drawable.voltage_normal;
        switch (standard) {
            case VOLTLOW:
            case VOLTSIDELOW:
                dr = R.drawable.voltage_sidelower;
                break;
            case VOLTHIGH:
                dr = R.drawable.voltage_lower;
                break;
            default:
                break;
        }
        return dr;
    }

    public int getStatusTextByStandard(Standard standard) {
        int sr = R.string.voltage_normal;
        switch (standard) {
            case VOLTLOW:
                sr = R.string.voltage_lower_electric;
                break;
            case VOLTSIDELOW:
                sr = R.string.voltage_lower;
                break;
            case VOLTHIGH:
                sr = R.string.voltage_hight;
                break;
            default:
                break;
        }
        return sr;
    }

    public int getVoltageProgress() {
        if (standardList == null) {
            return 0;
        }
        String standard = standardList.voltHigh;
        if (TextUtils.isEmpty(standard)) {
            standard = standardList.voltNormal;
        }
        List<Double> doubles = StringUtil.doubles(standard, "#");
        if (doubles == null || doubles.size() < 2) {
            return 0;
        }
        double max = doubles.get(1);
        if (max <= 0) {
            return 0;
        }
        return (int) (value / max * 100);
    }

    public int getVoltageWaveBgByStandard() {
        int dr = R.drawable.voltage_normal_wave_bg;
        switch (state) {
            case LOW:
                dr = R.drawable.voltage_lower_electric_wave_bg;
                break;
            case SIDE_LOW:
                dr = R.drawable.voltage_lower_wave_bg;
                break;
            /*case VOLTHIGH:
                dr = R.drawable.voltage_lower_electric_wave_bg;
                break;*/
            case EXCEPTION:
                dr = R.drawable.voltage_exception_wave_bg;
                break;
            default:
                break;
        }
        return dr;
    }

    public int getVoltageWaveBorderColorByStandard() {
        switch (state) {
            case LOW:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_border_lower_electric_color);
            case SIDE_LOW:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_front_lower_color);
            /*case VOLTHIGH:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_border_normal_color);*/
            case EXCEPTION:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_border_exception_color);
            default:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_border_normal_color);
        }
    }

    public int getVoltageWaveFrontColorByStandard() {
        switch (state) {
            case LOW:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_border_lower_electric_color);
            case SIDE_LOW:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_front_lower_color);
            /*case VOLTHIGH:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_border_normal_color);*/
            case EXCEPTION:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_front_exception_color);
            default:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_front_normal_color);
        }
    }

    public void setVoltageWaveColorByStandard(WaveProgressBar waveProgressBar){
        switch (state) {
            case LOW:
                waveProgressBar.setFrontWaveColor(ResUtil.getColor(R.color.vehicle_coltage_wave_border_lower_electric_color));
                waveProgressBar.setBehidWaveColor(ResUtil.getColor(R.color.vehicle_coltage_wave_front_lower_electric_color));
                break;
            case SIDE_LOW:
                waveProgressBar.setFrontWaveColor(ResUtil.getColor(R.color.vehicle_coltage_wave_border_lower_color));
                waveProgressBar.setBehidWaveColor(ResUtil.getColor(R.color.vehicle_coltage_wave_front_lower_color));
                break;
            /*case VOLTHIGH:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_border_normal_color);*/
            case EXCEPTION:
                waveProgressBar.setFrontWaveColor(ResUtil.getColor(R.color.vehicle_coltage_wave_front_exception_color));
                waveProgressBar.setBehidWaveColor(ResUtil.getColor(R.color.vehicle_coltage_wave_border_exception_color));
                break;
            default:
                waveProgressBar.setFrontWaveColor(ResUtil.getColor(R.color.vehicle_coltage_wave_front_normal_color));
                waveProgressBar.setBehidWaveColor(ResUtil.getColor(R.color.vehicle_coltage_wave_behind_normal_color));
        }
    }

    public int getVoltageWaveBehindColorByStandard() {
        switch (state) {
            case LOW:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_behind_lower_electric_color);
            case SIDE_LOW:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_behind_lower_color);
            /*case VOLTHIGH:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_border_normal_color);*/
            case EXCEPTION:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_behind_exception_color);
            default:
                return ResUtil.getColor(R.color.vehicle_coltage_wave_behind_normal_color);
        }
    }

    public static class Response extends BaseResponse<Voltage> {}
}
